<template>
    <div class="content-box">
        <div class="search-container">
            <el-input style="width: 200px" placeholder="请输入员工ID" suffix-icon="el-icon-search" v-model="id"></el-input>
            <el-input style="width: 200px" placeholder="请输入员工名字" suffix-icon="el-icon-user" class="ml-5"
                v-model="name"></el-input>
            <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>
        <!-- 表头 -->
        <div class="search-container">
            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info"
                icon-color="red" title="您确定批量删除这些数据吗？" @confirm="delBatch">
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
        </div>
        <!-- 表格 -->
        <div class="table-container">
            <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection"></el-table-column>
                <el-table-column prop="id" label="员工ID"></el-table-column>
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column prop="telephone" label="电话"></el-table-column>
                <el-table-column prop="password" label="密码"></el-table-column>
                <el-table-column prop="address" label="住址"></el-table-column>
                <el-table-column prop="entryTime" label="入职时间"></el-table-column>
                <el-table-column prop="leaveTime" label="离职时间"></el-table-column>
                <el-table-column label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit" circle></el-button>
                        <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info"
                            icon-color="red" title="您确定删除吗？" @confirm="del(scope.row.id)">
                            <el-button type="danger" slot="reference" icon="el-icon-delete" circle></el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNumber"
                :page-sizes="[2, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 新增模态款 -->
        <el-dialog title="新增员工" :visible.sync="dialogADD" width="30%">
            <el-form label-width="80px" size="small">
                <el-form-item label="姓名">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.telephone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="住址">
                    <el-input v-model="form.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="入职时间">
                    <el-input v-model="form.entryTime" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="离职时间">
                    <el-input v-model="form.leaveTime" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogADD = false">取 消</el-button>
                <el-button type="primary" @click="add()">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 编辑模态框 -->
        <el-dialog title="编辑员工" :visible.sync="dialogUpdate" width="30%">
            <el-form label-width="80px" size="small">
                <el-form-item label="姓名">
                    <el-input v-model="form1.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form1.telephone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form1.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="住址">
                    <el-input v-model="form1.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="入职时间">
                    <el-input v-model="form1.entryTime" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="离职时间">
                    <el-input v-model="form1.leaveTime" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogUpdate = false">取 消</el-button>
                <el-button type="primary" @click="update()">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>
    
<script>
import { getEmployeeList, deleteEmployee, deleteEmployees, addEmployee, updateEmployee } from "@/api/employee";
export default {
    name: "Employee",
    data() {
        return {
            tableData: [],
            id: '',
            name: '',
            ids: {},
            total: 0,
            pageNumber: 1,
            pageSize: 10,
            form: {},
            form1: {},
            searchForm: {
                pageNumber: 1, // 当前页数
                pageSize: 10, // 页面大小
            },
            dialogUpdate: false,
            dialogADD: false,
            multipleSelection: [],
            selection: [], // 添加selection属性
        }
    },
    mounted() {
        this.load();
    },
    methods: {
        //初始化数据
        load() {
            this.getDataList();
        },
        //搜索
        search() {
            this.searchForm.id = this.id;
            this.searchForm.name = this.name;
            this.load();
            this.searchForm.id = '';
            this.searchForm.name = '';
        },
        //重置
        reset() {
            this.id = "";
            this.name = "";
            this.search();
        },
        //复选框选中状态
        handleSelectionChange(selection) {
            this.selection = selection;
            let arr = [];
            for (var i = 0; i < selection.length; i++) {
                arr[i] = selection[i].id;
            }
            this.ids = arr + '';
            // console.log(this.ids)
        },

        handleSizeChange(pageSize) {
            this.searchForm.pageSize = pageSize;
            this.search();
        },
        handleCurrentChange(pageNum) {
            this.searchForm.pageNumber = pageNum;
            this.search();
        },

        // 获取数据
        getDataList() {
            // this.loading = true;
            getEmployeeList(this.searchForm).then((res) => {
                // console.log(res.result)
                if (res.success == true) {
                    this.tableData = res.result.records;
                    this.total = res.result.total;
                }
            });
        },
        // 新增显示模态款
        handleAdd() {
            this.dialogADD = true
            this.form = {}
        },
        // 编辑显示模态款
        handleEdit(row) {
            this.form1 = { ...row };
            this.dialogUpdate = true
        },
        //编辑员工
        update() {
            // 更新员工
            updateEmployee(this.form1.id, this.form1).then((res) => {
                if (res.success === true) {
                    this.$message.success("更新成功！");
                    this.dialogUpdate = false;
                    this.load();
                } else {
                    this.$message.error("更新失败！");
                }
            });
        },
        //新增员工 
        add() {
            // 添加新员工
            console.log(this.form)
            addEmployee(this.form).then((res) => {
                console.log(res);
                if (res.success === true) {
                    this.$message.success("新增成功！");
                    this.dialogADD = false;
                    this.load();
                } else {
                    this.$message.error("新增失败！");
                }
            });
        },
        // 删除单个用户
        del(id) {
            deleteEmployee(id).then((res) => {
                if (res.success === true) {
                    this.$message.success("删除成功！");
                    this.load();
                } else {
                    this.$message.error("删除失败！");
                }
            });
        },
        //删除多个用户
        delBatch() {
            deleteEmployees(this.ids).then((res) => {
                if (res.success == true) {
                    this.$message.success("全部删除成功！");
                    this.load();
                }
                else {
                    this.$message.error("全部删除失败！");
                }
            })
        }
    }
}
</script>
<style>
.headerBg {
    background: #eee !important;
}

.content-box {
    width: calc(100% - 10px);
    height: 100%;
    display: flex;
    flex-direction: column;
}

.table-container {
    flex: 1;
    overflow-y: auto;
}

.search-container {
    padding: 0, 10px;
}

.el-button.is-circle {
    margin-right: 10px;
}

.el-card__body,
.el-main {
    padding: 10px 20px 20px 20px;
}

.pagination-container {
    height: 30px;
    padding-top: 10px;
}
</style>